package com.shouhuobao.samecity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.collectplus.express.BaseActivity2;
import com.collectplus.express.R;
import com.collectplus.express.logic.AppCache;
import com.collectplus.express.logic.AppConfig;
import com.collectplus.express.logic.AppHttp;
import com.collectplus.express.logic.AppResult;
import com.collectplus.express.logic.JSON;
import com.collectplus.express.logic.MsgID;
import com.collectplus.express.model.SameCityHistoryBean;
import com.collectplus.express.tools.TextWatcherExt;
import com.shouhuobao.bhi.loc.BaiduLocationService;
import com.shouhuobao.bhi.login.LoginActivity;

import droid.frame.activity.title.TitleRes;
import droid.frame.utils.Android;
import droid.frame.utils.android.ImmUtils;

/**
 * 同城寄件地址
 * Created by lidandan on 2017/12/27.
 */
public class SameCityAddressActivity extends SameCityBaseActivityWithSearch implements View.OnClickListener {

    private ListView mListView;// 地址列表
    private SameCityAddressAdapter mAdapter;

    private boolean isFrom = false;//true 从SameCityInfoConfirmActivity启动当前类，进入当前界面
    private int type;//1 从计算价格界面进入

    public static String address;
    public static String phone;
    public static String name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isFrom = getIntent().getBooleanExtra("isFrom", false);
        type = getIntent().getIntExtra("type", 0);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.same_city_address);
        super.findViewById();
        getAppTitle().setCommonTitle("寄件地址", new TitleRes("历史记录", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                if (AppCache.isLogin() == false) {
                    intent.setClass(getContext(), LoginActivity.class);
                } else {
                    intent.setClass(getContext(), SameCityAddressHistoryActivity.class);
                }
                intent.putExtra("type", type);
                startActivityForResult(intent, 1201);
            }
        }));

        mListView = (ListView) findViewById(R.id.listview);

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                SameCityHistoryBean item = (SameCityHistoryBean) parent.getItemAtPosition(position);
                if (item == null) {
                    return;
                }
                if (type == 1) {
                    Intent intent = new Intent();
                    intent.putExtra("item", item);
                    setResult(Activity.RESULT_OK, intent);
                    finish();
                } else {
                    if (isFrom) {
                        Intent intent = new Intent();
                        intent.putExtra("item", item);
                        intent.putExtra("finish", false);
                        setResult(Activity.RESULT_OK, intent);
                        finish();
                    } else {
                        Intent intent = new Intent(getContext(), SameCityInfoConfirmActivity.class);
                        intent.putExtra("type", 0);
                        intent.putExtra("item", item);
                        if (isEmpty(address) && isEmpty(phone) && isEmpty(name)) {
                            intent.putExtra("loadType", 0);
                        } else {
                            intent.putExtra("loadType", 1);
                        }
                        startActivityForResult(intent, 1202);
                    }
                }
            }
        });
        mListView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                SameCityHistoryBean item = (SameCityHistoryBean) parent.getItemAtPosition(position);
                if (item == null) {
                    return;
                }
//                                        showLoadingDialog("查询中");
//                                        AppHttp.getInstance().getAddressSameCity(item.getLongitude(), item.getLatitude());
                // 切换视图
                onClick(mSearchOrCancel);
                if (type == 1) {
                    Intent intent = new Intent();
                    intent.putExtra("item", item);
                    setResult(Activity.RESULT_OK, intent);
                    finish();
                } else {
                    //点击跳转到确认界面，进行填写剩余的信息
                    if (isFrom) {
                        Intent intent = new Intent();
                        intent.putExtra("item", item);
                        intent.putExtra("finish", false);
                        setResult(Activity.RESULT_OK, intent);
                        finish();
                    } else {
                        Intent intent = new Intent(getContext(), SameCityInfoConfirmActivity.class);
                        intent.putExtra("type", 0);
                        intent.putExtra("item", item);
                        intent.putExtra("loadType", 0);
                        startActivityForResult(intent, 1202);
                    }
                }
            }
        });
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MsgID.location_baidu:
                cancelLoadingDialog();
                BDLocation mBdLocation = (BDLocation) msg.obj;
                if (mBdLocation != null) {
                    if (AppConfig.isOtherCity != 0) {
                        mCurrentCityName = AppConfig.getCityName();
                    } else {
                        mCurrentCityName = mBdLocation.getCity();
                    }
                    showLoadingDialog("正在查询");
                    AppHttp.getInstance().getAddressSameCity(mBdLocation.getLongitude(), mBdLocation.getLatitude());
                } else {
                    showToast("定位失败");
                }
                return false;
            // 查询地址上门地址
            case MsgID.same_city_address_location:
                cancelLoadingDialog();
                final AppResult<SameCityHistoryBean> result1 = JSON.parseList(msg.obj, SameCityHistoryBean.class);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (result1.isSuccess()) {
                            mAdapter = new SameCityAddressAdapter(result1.getResultList(), getContext());
                            mListView.setAdapter(mAdapter);
                        } else {
                            mAdapter = new SameCityAddressAdapter(null, getContext());
                            mListView.setAdapter(mAdapter);
                            showToast(result1.getMessage());
                        }
                    }
                });
                return false;
            default:
                return super.handleMessage(msg);
        }

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        //跳转到历史记录回传
        if (resultCode == Activity.RESULT_OK && requestCode == 1201) {
            if (type == 1) {
                SameCityHistoryBean cityHistoryBean = (SameCityHistoryBean) data.getSerializableExtra("item");
                if (cityHistoryBean != null) {
                    Intent intent = new Intent();
                    intent.putExtra("item", cityHistoryBean);
                    setResult(RESULT_OK, intent);
                }
            } else {
                if (isFrom) {
                    Intent intent = new Intent();
                    intent.putExtra("finish", true);
                    setResult(RESULT_OK, intent);
                } else {
                    setResult(RESULT_OK);
                }
            }
            finish();
        }
        //跳转到确认地址界面
        else if (resultCode == Activity.RESULT_OK && requestCode == 1202) {
            setResult(RESULT_OK);
            finish();
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        address = null;
        phone = null;
        name = null;
    }
}

